package mta.command.generate;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

/**
 * Magento Translation Assistant
 * Copyright (c) 2009 Dmitry Nikolaev
 * Contact author: blackliteon@gmail.com
 */
public class GeneratePatchBuilderOptions {
    private String[] args;
    private CommandLine cl;
    private SelectedPhrases selector;

    public enum SelectedPhrases {
        Untranslated, ByTemplate
    }

    public GeneratePatchBuilderOptions(String[] args) {
        this.args = args;
    }

    private SelectedPhrases parseTypeOfSelectedPhrases() throws ParseException {
        int count = 0;
        SelectedPhrases result = null;

        if (cl.hasOption("t")) {
            result = SelectedPhrases.ByTemplate;
            count++;
        }

        if (cl.hasOption("u")) {
            result = SelectedPhrases.Untranslated;
            count++;
        }

        if (count == 1) {
            return result;
        } else {
            throw new ParseException("Choose type of selected phrases (untranslated or by template)");
        }
    }

    public void parse() throws ParseException {

        CommandLineParser parser = new PosixParser();
        cl = parser.parse(new GeneratePatchOptions().getOptions(), args);

        if (!cl.hasOption("f") || !cl.hasOption("l")) {
            throw new ParseException("You need to specify 'file' and 'localization'");
        }

        selector = parseTypeOfSelectedPhrases();
    }

    public String getFilePath() {
        return cl.getOptionValue("f");
    }

    public String getLocalizationPath() {
        return cl.getOptionValue("l");
    }

    public SelectedPhrases getSelectorType() {
        return selector;
    }

    public String getTemplate() {
        return cl.getOptionValue("t");
    }


}